/**
 * tools mudule 一些公用的工具方法
 * @module common/tools
 */

/** 检测是否登录 */
exports.checklogin = (req, res, next) => {
  if (req.session.user) {
    next();
  } else {
    console.log("测试登陆未通过")
    res.redirect('/dev/sign') 
  }
}

exports.signin = (req, res, next) => {
  let username = req.body.username;
  console.log(username)
  let password = req.body.password;
  
  if (username == 'admin' && password == 'admin') {
    req.session.regenerate(function (err) {
      if (err) {
        console.log(`${username}的session获取失败`)
      } else {
        req.session.user = "admin";
        req.session.save();
        res.redirect('/dev')
      }
    })
  } else {
    res.end("用户密码错误")
  }
  
}

exports.signout = (req, res, next) => {
  if (req.session.user) {
    req.session.destory(function (err) {
      if (err) console.log(`session销毁失败`);
      else console.log(`session被销毁`)
    })
  } else {
    console.log(`现在用户不是登陆状态`)
    next();
  }
}